package problem.ejercicio2;

import static org.junit.Assert.assertEquals;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Vector;

import org.junit.Test;


public class RedFileTests {
	
	@Test
	   public void exampleFileRunTest() throws IOException {
		// Con este test se lee el archivo de entrada proporcioando por la catedra 
	    // y se genera la salida
	      BufferedReader is = new BufferedReader( new InputStreamReader( getClass().getResourceAsStream( "Tp1Ej2.in" ) ) );
	      BufferedWriter os = new BufferedWriter( new FileWriter("src/problem/ejercicio2/Tp1Ej2Mio.out" ));
	      
	      String line;
	      while ( ( line = is.readLine() ) != null ) {
	         os.append( run( line, is.readLine() ) ).append( '\n' );
	      }
	      os.close();
	      
	   }


	  @Test
	   public void fullFileRunTest() throws IOException {
		  // Con este test se comparan si 2 archivos de tipo salida son iguales
	      BufferedReader source  = new BufferedReader( new InputStreamReader( getClass().getResourceAsStream( "Tp1Ej2Mio.out" ) ) );
	      BufferedReader control = new BufferedReader( new InputStreamReader( getClass().getResourceAsStream( "Tp1Ej2.out" ) ) );
	      
	      String line;
	      while ( ( line = source.readLine() ) != null ) {
	    	  runEnteros( line, control.readLine() );
	      }
	      
	   }
	  
	 /// ------------------------------------------------------------------ ///
	  
	// Comparo q sean iguales los 2 numeros
	   private void runEnteros( String a, String b ) {
		   Vector<String> s = parseEnteros(a);
		   Vector<String> c = parseEnteros(b);		   
		   assertEquals(s.get(0),c.get(0));		         
	   }
	   
	// Aca tengo un entero
	   public static Vector<String> parseEnteros( String line ) {
		      Vector<String> in = new Vector<String>();

		      StringTokenizer st = new StringTokenizer( line, " " );
		      while ( st.hasMoreTokens() ) {
		         in.add( (st.nextToken()));
		      }

		      return in;
		}      
	   

	   // Mando a probar las 2 lineas recibidas
	   private String run( String a, String b ) {
		   Vector<String> vs = parseInvestigadores(a);
		   Vector<Amistad> amistades =  parseAmistades(b);
		   Red rd = new Red();
		   return String.valueOf(rd.run(vs.get(0),vs.get(1), amistades));	      
	   }
	   
	   // Aca tengo p y q
	   public static Vector<String> parseInvestigadores( String line ) {
		      Vector<String> in = new Vector<String>();

		      StringTokenizer st = new StringTokenizer( line, " " );
		      while ( st.hasMoreTokens() ) {
		         in.add( st.nextToken());
		      }

		      return in;
		   }   
	  
	   
	   // Aca tengo las relaciones definidas
	   public static Vector<Amistad> parseAmistades( String line ) {
	         Vector<Amistad> lst = new Vector<Amistad>();	        
	         
	         StringTokenizer st = new StringTokenizer( line, ";" );
	         while ( st.hasMoreTokens() ) {
	            lst.add( parseAmistad( st.nextToken() ) );
	         }

	         return lst;
	    }

	    private static Amistad parseAmistad( String string ) {
	         StringTokenizer st = new StringTokenizer( string, " " );
	         return new Amistad( st.nextToken() , st.nextToken());
	    } 
	  
	
}
